import { PureComponent, Fragment } from "react";
import { Picker, List } from "antd-mobile"
import { isNull, handleSelect } from "../../utils/helper"
import MyInputNumberModal from "./MyForm/MyInputNumberModal"
interface Props{
	isReset:boolean,
	formData:object,
	defaultFormData:object,
	dataset_key?:string,
	getFieldDecorator:Function,
	getFieldValue:Function,
	setFieldsValue:Function,
	field:string[],
	label?:string,
	required?:any,
	onChange?:Function
}
interface State{
	hasError:boolean,
	errorMsg:string
}
export default class MiInputNumberModal extends PureComponent<Props,State> {
	public readonly state:Readonly<State>={
		hasError:false,
		errorMsg:""
	}
	
	componentWillReceiveProps(next:Props) {
		let { isReset: oldReset} = this.props;
		let { isReset: newReset} = next;
		if (oldReset !== newReset && newReset) {
			this.setState({
				hasError: false
			})
		}
	}

	componentDidMount() {

	}
	render() {
		let { getFieldDecorator, field, defaultFormData, label, required  } = this.props;
		let { hasError, errorMsg } = this.state;
		let isRequired = required == 1;
		return (
			<div>
				{
					getFieldDecorator(field[0], {
						initialValue: defaultFormData[field[0]],
						rules: [
							{
								validator: (rule, value, callback) => {
									if (isRequired) {
										if (isNull(value)) {
											if (!hasError) {
												this.setState({
													hasError: true,
													errorMsg: "请选择：" + label
												})
												callback("请选择：" + label)
											}
										}
										else {
											this.setState({
												hasError: false,
												errorMsg: ""
											})
											callback()
										}
									}
									callback()
								}
							}
						]
					})(
						<MyInputNumberModal {...this.props} hasError={hasError} errorMsg={errorMsg} />
					)
				}
			</div>
		)
	}
}
